package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Negocio2.Disciplina;
import Negocio2.Professor;;

public class ProfessorDAO {
	private Connection conexao;
	public ProfessorDAO() throws SQLException{
		this.conexao = CriaConexao.getConexao();
	}
	public void adiciona(Professor professor)throws SQLException{
		
		//Prepara conex�o
		String sql = "insert into professores(nome,matricula)"+
		"values(?,?)";
		PreparedStatement stmt = conexao.prepareStatement(sql);
		
		
		//Seta os valores
		stmt.setString(1, professor.getNome());
		stmt.setString(2, professor.getMatricula());
		
		
		//Executa o c�digo SQL
		stmt.execute();
		stmt.close();
	}
	
	public List<Professor> getLista() throws SQLException{
		String sql = "select * from professores";
		PreparedStatement stmt = this.conexao.prepareStatement(sql);
		ResultSet rs = stmt.executeQuery();
		
		List<Professor> listaProfessores = new ArrayList<Professor>();
		
		while(rs.next()){
			Professor professor = new Professor();
			professor.setNome(rs.getString("nome"));
			professor.setMatricula(rs.getString("matricula"));
            professor.setCodigo(rs.getString("codigo"));
			listaProfessores.add(professor);
		}
		rs.close();
		stmt.close();
		return listaProfessores;
	}
	
	public void altera(Professor professor) throws SQLException{
		String sql = "update professores set nome=?, matricula=? where matricula=?";
		PreparedStatement stmt = conexao.prepareStatement(sql);	
		
		//Seta os valores
		stmt.setString(1, professor.getNome());
		stmt.setString(2, professor.getMatricula());
		stmt.setString(3, professor.getMatricula());
		
		//Executa o c�digo SQL
		stmt.execute();
		stmt.close();
	}
	
	public void remove(Professor professor) throws SQLException{
		String sql = "delete from professores where matricula =?";
		PreparedStatement stmt = conexao.prepareStatement(sql);
		stmt.setString(1, professor.getMatricula());
		stmt.execute();
		stmt.close();
	}

        public void adicionaDisciplina(Disciplina disciplina, Professor professor)throws SQLException{

		//Prepara conex�o
		String sql = "insert into disciplinasProfessor(cod_disciplina,cod_professor)"+
		"values(?,?)";
		PreparedStatement stmt = conexao.prepareStatement(sql);


		//Seta os valores
		stmt.setString(1, disciplina.getCodigo());
		stmt.setString(2, professor.getCodigo());


		//Executa o c�digo SQL
		stmt.execute();
		stmt.close();
	}

    public void adicionaDisciplinaProximoSemestre(Disciplina disciplina, Professor professor)throws SQLException{

		//Prepara conex�o
		String sql = "insert into disciplinasProfessorProx(cod_disciplina,cod_professor)"+
		"values(?,?)";
		PreparedStatement stmt = conexao.prepareStatement(sql);


		//Seta os valores
		stmt.setString(1, disciplina.getCodigo());
		stmt.setString(2, professor.getCodigo());


		//Executa o c�digo SQL
		stmt.execute();
		stmt.close();
	}

}
